// ==UserScript==
// @name        MonkeyPatch
// @namespace   Violentmonkey Scripts
// @match       https://web.whatsapp.com/
// @downloadURL https://gitee.com/happycoding/violence-plug-in/raw/master/scripts/MonkeyPatch.user.js
// @grant       none
// @version     1.1.2
// @run-at      document-end
// @author      $搬运工
// @description 2022/8/17 12:27:25
// ==/UserScript==



(function() {
  
const log = console.log.bind(console)

const parentElements = function(nodeList) {
  const l = nodeList.length
  const r = []
  for(let i = 0; i < l; ++i) {
    r.push(nodeList[i].parentElement)
  }
  return r
}

const starFromMobile = function(str) {
  const reg = /\+[-\s\d]{11,16}/gm
  const repl = "+ ** *** ********"
  if (str.replaceAll) {
    const i = str.search(reg)
    if (i >= 0 ) {
      return str.replaceAll(reg, repl)
    }
  }
  return null
}

const hideMobile = function(element) {
  const t = element.title
  const c = element.innerText
  const l = element.children.length
  
  const nt = starFromMobile(t)
  const nc = starFromMobile(c)
  if (nt !== null) {
    element.tittle = nt   
  } else if (l !== 0) {
    return
  }
  if (nc !== null) {
    element.innerText = nc
    element.title = ''
    element.ariaLabel = ''
  }

}

const hideAll = function(nodeList) {
  const l = nodeList.length
  if (l <= 0) {
    return
  }
  for(let i = 0; i < l; ++i) {
    hideMobile(nodeList[i])
  }
} 

const _play = function() {
  const f1 = document.querySelectorAll("#pane-side span")
  const f2 = document.querySelectorAll("#main span")
  const f3 = document.querySelectorAll(".copyable-area span")
  const f4 = document.querySelectorAll('ul li div[role="button"]')
  const t = document.querySelectorAll('#pane-side .matched-text')
  const f5 = parentElements(t)
  
  hideAll(f1)
  hideAll(f2)
  hideAll(f3)
  hideAll(f4)
  hideAll(f5)
}

const playload = function() {
    const f1 = document.querySelectorAll("#pane-side div[role='gridcell']>div>span")
    const f2 = document.querySelectorAll("#pane-side span[data-testid='last-msg-status']")
    const f3 = document.querySelectorAll("div[data-testid='cell-frame-container'] span[dir='auto']")
    const f4 = document.querySelectorAll("#main span")
    const f5 = document.querySelectorAll('span.selectable-text,span.copyable-text span')
    hideAll(f1)   
    hideAll(f2)
    hideAll(f3)   
    hideAll(f4)
    hideAll(f5) 
}

function elementReady(selector) {
  return new Promise((resolve, reject) => {
    let el = document.querySelector(selector);
    if (el) {
      resolve(el); 
      return
    }
    new MutationObserver((mutationRecords, observer) => {
      // Query for elements matching the specified selector
      Array.from(document.querySelectorAll(selector)).forEach((element) => {
        resolve(element);
        //Once we have resolved we don't need the observer anymore.
        observer.disconnect();
      });
    })
      .observe(document.documentElement, {
        childList: true,
        subtree: true
      });
  });
}

const repeat = function(fn) {
  const p = fn
  p()
  setTimeout(p, 100)
  setTimeout(p, 200)
  setTimeout(p, 400)
  setTimeout(p, 600)
  //setTimeout(p, 1000)
}

const disableNotify = function () {
  window.Notification = function() {}
}

class ReplNotify extends Notification {
  constructor(title, options) {
    const t = starFromMobile(title)
    const o = options.body
    const b = o ? starFromMobile(o) : o
    if ( t !== null ) {
      title = t
    }
    if ( b !== null ) {
      options.body = b
    }
    super(title, options)
  }
}

const __main = function() {
  window.Notification = ReplNotify
  
  const p = _play
  elementReady('header').then(function() {
    console.log('loaded')
    repeat(p)
  })
  
  const app = document.querySelector('#app')
  app.addEventListener('mousedown' ,function(e){
    console.log('mousedown')
    repeat(p)
  })
  app.addEventListener('keydown' ,function(e){
    console.log('keydown')
    repeat(p)
  })
  document.addEventListener('wheel' ,function(e){
    console.log('wheel')
    repeat(p)
  })
  
}

__main()
})();